<html lang="en-US">

<head>
    
<title>Go 面试官：Go 结构体是否可以比较，为什么？ - 花落雨忧</title>

<meta property="og:title" content="Go 面试官：Go 结构体是否可以比较，为什么？ - 花落雨忧">



    



    
    <meta property="description" content="大家好，我是煎鱼。
最近金三银四，是面试的季节。在我的 Go 读者交流群里出现了许多小伙伴在讨论自己面试过程中所遇到的一些 Go 面试题。
今天的男主角，是 Go 工程师的必修技能，也是极容易踩坑的地方，就是 “Go 面试题：Go 结构体（struct）是否可以比较？”
如果可以比较，是为什么？如果不可以比较，又是为什么？
请在此处默念自己心目中的答案，再往和煎鱼一起研讨一波 Go 的技术哲学。 &amp;hellip;">
    <meta property="og:description" content="大家好，我是煎鱼。
最近金三银四，是面试的季节。在我的 Go 读者交流群里出现了许多小伙伴在讨论自己面试过程中所遇到的一些 Go 面试题。
今天的男主角，是 Go 工程师的必修技能，也是极容易踩坑的地方，就是 “Go 面试题：Go 结构体（struct）是否可以比较？”
如果可以比较，是为什么？如果不可以比较，又是为什么？
请在此处默念自己心目中的答案，再往和煎鱼一起研讨一波 Go 的技术哲学。 &amp;hellip;">
    






<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">

<link rel="shortcut icon" href="https://www.lican.asia/logo/logo.png" type="image/x-icon" />



<link rel="stylesheet" href="/css/style.min.css" />

<link rel="stylesheet" href="/css/reset.min.css" />




<script src="https://www.lican.asia/js/highlight.min.js"></script>

<script>
hljs.configure({ ignoreUnescapedHTML: true })
hljs.highlightAll();
</script>


<script src="https://www.lican.asia/js/jquery.min.js"></script>




<link href="https://www.lican.asia/css/hugo-code.min.css" rel="stylesheet" />



    <style>
        .post-content img {
            max-width: 400px;
        }
    </style>
</head>

<body id="period" class="home blog">
    <a class="skip-content" href="#main">Press "Enter" to skip to content</a>
    <div id="overflow-container" class="overflow-container">
        <header class="site-header" id="site-header" role="banner">
    <div class="max-width">
        <div id="title-container" class="title-container">
            <div id="site-title" class="site-title"><a href="/">花落雨忧</a></div>
            <p class="tagline">思所及 力所行 方为真.</p>
        </div>
        
        <div id="menu-primary-container" class="menu-primary-container">
            <div class="icon-container">
            </div>
            <div id="menu-primary" class="menu-container menu-primary" role="navigation">
                <nav class="menu">
                    <ul id="menu-primary-items" class="menu-primary-items">
                        
                        
                        <li id="menu-item-0"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/posts" aria-current="page" tabindex="0">首页</a></li>
                        
                        <li id="menu-item-1"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/tech/" aria-current="page" tabindex="1">技术文档</a></li>
                        
                        <li id="menu-item-2"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/article/" aria-current="page" tabindex="2">文章</a></li>
                        
                        <li id="menu-item-3"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/project/" aria-current="page" tabindex="3">项目</a></li>
                        
                        <li id="menu-item-4"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/about/" aria-current="page" tabindex="4">关于</a></li>
                        
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</header>

        <div id="primary-container" class="primary-container">
            <div class="max-width">
                <section id="main" class="main" role="main">
                    <div id="loop-container" class="loop-container">
                        <div
                            class="post type-post status-publish format-standard hentry entry">
                            <article>
                                <div class="post-container">
                                    <div class="post-header">
                                        <h2 class="post-title">
                                            <a href="/posts/posts/go/go-tips-sturct/">Go 面试官：Go 结构体是否可以比较，为什么？</a>
                                        </h2>
                                        
                                        <div class="post-byline">Published on
                                            <a class="date" href="javascript:;">2021/04/05</a>
                                            
                                            
                                            
                                            
                                            
                                        
                                    </div>
                                    <div class="post-content">
                                        <p>大家好，我是煎鱼。</p>
<p>最近金三银四，是面试的季节。在我的 Go 读者交流群里出现了许多小伙伴在讨论自己面试过程中所遇到的一些 Go 面试题。</p>
<p>今天的男主角，是 Go 工程师的必修技能，也是极容易踩坑的地方，就是 “<strong>Go 面试题：Go 结构体（struct）是否可以比较？</strong>”</p>
<p>如果可以比较，是为什么？如果不可以比较，又是为什么？</p>
<p><strong>请在此处默念自己心目中的答案</strong>，再往和煎鱼一起研讨一波 Go 的技术哲学。</p>
<h2 id="结构体是什么">结构体是什么</h2>
<p>在 Go 语言中有个基本类型，开发者们称之为结构体（struct）。是 Go 语言中非常常用的，基本定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-golang" data-lang="golang"><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">struct_variable_type</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">member</span> <span style="color:#a6e22e">definition</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">member</span> <span style="color:#a6e22e">definition</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">member</span> <span style="color:#a6e22e">definition</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>简单示例：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-golang" data-lang="golang"><span style="display:flex;"><span><span style="color:#f92672">package</span> <span style="color:#a6e22e">main</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> <span style="color:#e6db74">&#34;fmt&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Vertex</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Name1</span> <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Name2</span> <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">v</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">Vertex</span>{<span style="color:#e6db74">&#34;脑子进了&#34;</span>, <span style="color:#e6db74">&#34;煎鱼&#34;</span>}
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">v</span>.<span style="color:#a6e22e">Name2</span> = <span style="color:#e6db74">&#34;蒸鱼&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">v</span>.<span style="color:#a6e22e">Name2</span>)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>输出结果：</p>
<pre tabindex="0"><code>蒸鱼
</code></pre><p>这部分属于基础知识，因此不再过多解释。如果看不懂，建议重学 Go 语言语法基础。</p>
<h2 id="比较两下">比较两下</h2>
<h3 id="例子一">例子一</h3>
<p>接下来正式开始研讨 Go 结构体比较的问题，第一个例子如下：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-golang" data-lang="golang"><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Value</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Name</span>   <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Gender</span> <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">v1</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">Value</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;煎鱼&#34;</span>, <span style="color:#a6e22e">Gender</span>: <span style="color:#e6db74">&#34;男&#34;</span>}
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">v2</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">Value</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;煎鱼&#34;</span>, <span style="color:#a6e22e">Gender</span>: <span style="color:#e6db74">&#34;男&#34;</span>}
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">v1</span> <span style="color:#f92672">==</span> <span style="color:#a6e22e">v2</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;脑子进煎鱼了&#34;</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;脑子没进煎鱼&#34;</span>)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>我们声明了两个变量，分别是 v1 和 v2。其都是 <code>Value</code> 结构体的实例化，是同一个结构体的两个实例。</p>
<p>他们的比较结果是什么呢，是输出 ”脑子进煎鱼了“，还是 ”脑子没进煎鱼“？</p>
<p>输出结果：</p>
<pre tabindex="0"><code>脑子进煎鱼了
</code></pre><p>最终输出结果是 ”脑子进煎鱼了“，初步的结论是可以结构体间比较的。皆大欢喜，那这篇文章是不是就要结束了？</p>
<p>当然不是&hellip;很多人都会踩到这个 Go 语言的坑，<strong>真实情况是结构体是可比较，也不可比较的</strong>，不要误入歧途了，这是一个非常 &ldquo;有趣&rdquo; 的现象。</p>
<h3 id="例子二">例子二</h3>
<p>接下来继续改造上面的例子，我们在原本的结构体中增加了指针类型的引用。</p>
<p>第二个例子如下：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-golang" data-lang="golang"><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Value</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Name</span>   <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Gender</span> <span style="color:#f92672">*</span><span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">v1</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">Value</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;煎鱼&#34;</span>, <span style="color:#a6e22e">Gender</span>: new(<span style="color:#66d9ef">string</span>)}
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">v2</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">Value</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;煎鱼&#34;</span>, <span style="color:#a6e22e">Gender</span>: new(<span style="color:#66d9ef">string</span>)}
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">v1</span> <span style="color:#f92672">==</span> <span style="color:#a6e22e">v2</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;脑子进煎鱼了&#34;</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;脑子没进煎鱼&#34;</span>)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>这段程序输出结果是什么呢，我们猜测一下，变量依然是同一结构体的两个实例，值的赋值方式和内容都是一样的，是否应当输出 “脑子进煎鱼了”？</p>
<p>答案是：脑子没进煎鱼。</p>
<h3 id="例子三">例子三</h3>
<p>我们继续不信邪，试试另外的基本类型，看看结果是不是还是相等的。</p>
<p>第三个例子如下：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-golang" data-lang="golang"><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Value</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Name</span>   <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">GoodAt</span> []<span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">v1</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">Value</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;煎鱼&#34;</span>, <span style="color:#a6e22e">GoodAt</span>: []<span style="color:#66d9ef">string</span>{<span style="color:#e6db74">&#34;炸&#34;</span>, <span style="color:#e6db74">&#34;煎&#34;</span>, <span style="color:#e6db74">&#34;蒸&#34;</span>}}
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">v2</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">Value</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;煎鱼&#34;</span>, <span style="color:#a6e22e">GoodAt</span>: []<span style="color:#66d9ef">string</span>{<span style="color:#e6db74">&#34;炸&#34;</span>, <span style="color:#e6db74">&#34;煎&#34;</span>, <span style="color:#e6db74">&#34;蒸&#34;</span>}}
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">v1</span> <span style="color:#f92672">==</span> <span style="color:#a6e22e">v2</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;脑子进煎鱼了&#34;</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;脑子没进煎鱼&#34;</span>)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>这段程序输出结果是什么呢？</p>
<p>答案是：</p>
<pre tabindex="0"><code># command-line-arguments
./main.go:15:8: invalid operation: v1 == v2 (struct containing []string cannot be compared)
</code></pre><p>程序运行就直接报错，IDE 也提示错误，一只煎鱼都没能输出出来。</p>
<h3 id="例子四">例子四</h3>
<p>那不同结构体，相同的值内容呢，能否进行比较？</p>
<p>第四个例子：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-golang" data-lang="golang"><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Value1</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Name</span> <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Value2</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Name</span> <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">v1</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">Value1</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;煎鱼&#34;</span>}
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">v2</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">Value2</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;煎鱼&#34;</span>}
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">v1</span> <span style="color:#f92672">==</span> <span style="color:#a6e22e">v2</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;脑子进煎鱼了&#34;</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;脑子没进煎鱼&#34;</span>)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>显然，会直接报错：</p>
<pre tabindex="0"><code># command-line-arguments
./main.go:18:8: invalid operation: v1 == v2 (mismatched types Value1 and Value2)
</code></pre><p>那是不是就完全没法比较了呢？并不，我们可以借助强制转换来实现：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-golang" data-lang="golang"><span style="display:flex;"><span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">v1</span> <span style="color:#f92672">==</span> <span style="color:#a6e22e">Value1</span>(<span style="color:#a6e22e">v2</span>) {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;脑子进煎鱼了&#34;</span>)
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">return</span>
</span></span><span style="display:flex;"><span>	}
</span></span></code></pre></div><p>这样程序就会正常运行，且输出 “脑子进煎鱼了”。当然，若是不可比较类型，依然是不行的。</p>
<h2 id="为什么">为什么</h2>
<p>为什么 Go 结构体有的比较就是正常，有的就不行，甚至还直接报错了。难道是有什么 “潜规则” 吗？</p>
<p>在 Go 语言中，Go 结构体有时候并不能直接比较，当其基本类型包含：slice、map、function 时，是不能比较的。若强行比较，就会导致出现例子中的直接报错的情况。</p>
<p>而指针引用，其虽然都是 <code>new(string)</code>，从表象来看是一个东西，但其具体返回的地址是不一样的。</p>
<p>因此若要比较，则需改为：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-golang" data-lang="golang"><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">gender</span> <span style="color:#f92672">:=</span> new(<span style="color:#66d9ef">string</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">v1</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">Value</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;煎鱼&#34;</span>, <span style="color:#a6e22e">Gender</span>: <span style="color:#a6e22e">gender</span>}
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">v2</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">Value</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;煎鱼&#34;</span>, <span style="color:#a6e22e">Gender</span>: <span style="color:#a6e22e">gender</span>}
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>这样就可以保证两者的比较。如果我们被迫无奈，被要求一定要用结构体比较怎么办？</p>
<p>这时候可以使用反射方法 <code>reflect.DeepEqual</code>，如下：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-golang" data-lang="golang"><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">v1</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">Value</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;煎鱼&#34;</span>, <span style="color:#a6e22e">GoodAt</span>: []<span style="color:#66d9ef">string</span>{<span style="color:#e6db74">&#34;炸&#34;</span>, <span style="color:#e6db74">&#34;煎&#34;</span>, <span style="color:#e6db74">&#34;蒸&#34;</span>}}
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">v2</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">Value</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;煎鱼&#34;</span>, <span style="color:#a6e22e">GoodAt</span>: []<span style="color:#66d9ef">string</span>{<span style="color:#e6db74">&#34;炸&#34;</span>, <span style="color:#e6db74">&#34;煎&#34;</span>, <span style="color:#e6db74">&#34;蒸&#34;</span>}}
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">DeepEqual</span>(<span style="color:#a6e22e">v1</span>, <span style="color:#a6e22e">v2</span>) {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;脑子进煎鱼了&#34;</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;脑子没进煎鱼&#34;</span>)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>这样子就能够正确的比较，输出结果为 “脑子进煎鱼了”。</p>
<p>例子中所用到的反射比较方法 <code>reflect.DeepEqual</code> 常用于判定两个值是否深度一致，其规则如下：</p>
<ul>
<li>相同类型的值是深度相等的，不同类型的值永远不会深度相等。</li>
<li>当数组值（array）的对应元素深度相等时，数组值是深度相等的。</li>
<li>当结构体（struct）值如果其对应的字段（包括导出和未导出的字段）都是深度相等的，则该值是深度相等的。</li>
<li>当函数（func）值如果都是零，则是深度相等；否则就不是深度相等。</li>
<li>当接口（interface）值如果持有深度相等的具体值，则深度相等。</li>
<li>&hellip;</li>
</ul>
<p>更具体的大家可到 <code>golang.org/pkg/reflect/#DeepEqual</code> 进行详细查看：</p>
<p><img src="https://image.eddycjy.com/aefb19ae4bce5bbbada39244141bfd68.jpg" alt="reflect.DeepEqual 完整说明"></p>
<p>该方法对 Go 语言中的各种类型都进行了兼容处理和判别，由于这不是本文的重点，因此就不进一步展开了。</p>
<h2 id="总结">总结</h2>
<p>在本文中，我们针对 Go 语言的结构体（struct）是否能够比较进行了具体例子的展开和说明。</p>
<p>其本质上还是对 Go 语言基本数据类型的理解问题，算是变形到结构体中的具体进一步拓展。</p>
<p>不知道你有没有在 Go 结构体吃过什么亏呢，欢迎在下方评论区留言和我们一起交流和讨论。</p>


                                        
                                        
                                        
                                        <div class="rp4wp-related-posts">
                                            <h3>相关文章</h3>
                                            <ul>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/go-tips-gmp-p/">Go 面试官：GMP 模型，为什么要有 P？</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/go-tips-goroutineid/">Go 群友提问：进程、线程都有 ID，为什么 Goroutine 没有 ID？</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/go-tips-interface/">Go 面试官：Go interface 的一个 “坑” 及原理分析</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/go-tips-defer/">Go 群友提问：学习 defer 时很懵逼，这道不会做！</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/go-tips-lenstr/">问个 Go 问题，字符串 len 为 0 和 字符串为空 ，有啥区别？</a>
                                                    </div>
                                                </li>
                                                
                                            </ul>
                                        </div>
                                        
                                        
                                    </div>

                                    
                                    
                                    

                                    
                                    <div class="post-meta">
                                        
                                        
                                        <div class="post-tags">
                                            <ul>
                                            
                                            <li>
                                                <a href="/tags/go" title="View all posts tagged match">go</a>
                                            </li>
                                            
                                            <li>
                                                <a href="/tags/%e9%9d%a2%e8%af%95%e9%a2%98" title="View all posts tagged match">面试题</a>
                                            </li>
                                            
                                            </ul>
                                        </div>
                                        
                                        
                                        <nav class="further-reading">
                                            
                                            <div class="previous">
                                                <span>&lt;&lt; Prev</span>
                                                <a href="https://www.lican.asia/posts/posts/go/go-tips-gmp-p/"
                                                    rel="prev">Go 面试官：GMP 模型，为什么要有 P？</a> </div>
                                            
                                            
                                            <div class="next">
                                                <span>Next >></span>
                                                <a href="https://www.lican.asia/posts/posts/go/go-tips-timer-memory/">Go 内存泄露之痛，这篇把 Go timer.After 问题根因讲透了！</a> 
                                            </div>
                                            
                                        </nav>
                                    </div>
                                    
                                    

                                    
                                    
                                    

                                </div>
                            </article>
                        </div>
                    </div>
                </section>
                <aside class="sidebar sidebar-primary" id="sidebar-primary" role="complementary">
    <h1 class="screen-reader-text">Sidebar</h1>
    

    
    
    <section id="text-2" class="widget widget_text">
        <div class="textwidget">
            
            <div id="profile">
                <div id="profile_picture"><img src="https://www.lican.asia/logo/logo.png"></div>
                <div id="profile_intro">
                    <p><span class="name">Lican</span></p>
                    <p class="intro">全栈开发者，爱好造轮子。</p>
                </div>
            </div>
            
            <p>
                <script type="text/javascript">
                    (function ($) {
                        $(document).ready(function () {
                            var menuPrimaryContainer = $('#menu-primary-container');
                            var profile = $('#text-2');
                            $('#toggle-navigation').click(function () {
                                if (menuPrimaryContainer.hasClass('open')) {
                                    profile.removeClass('open');
                                } else {
                                    profile.addClass('open');
                                }
                            });
                        });
                    })(jQuery);
                </script>
            </p>
        </div>
    </section>
    
    
    
    
    
    <section id="text-5" class="widget widget_text">
        <h2 class="widget-title">开源项目</h2>
        <div class="textwidget">
            <div id="projects" style="line-height: 22px;">
                
                <a href="https://github.com/idoubi/gonews"
                    target="_blank">gonews</a>: &nbsp;Daily news for golang<br>
                
                <a href="https://github.com/idoubi/sql2struct"
                    target="_blank">sql2struct</a>: &nbsp;Generate go struct according to SQL<br>
                
                <a href="https://github.com/idoubi/goz"
                    target="_blank">goz</a>: &nbsp;Request library used in golang<br>
                
        </div>
    </section>
    
    

    
    
    
    
    <section id="qrcode" class="widget widget_media_image">
        <h2 class="widget-title">微信公众号</h2>
        <img width="258" height="258"
            src="https://www.lican.asia/wechat/lican.png"
            class="image wp-image-5514  attachment-full size-full" alt=""
            style="max-width: 100%; height: auto;"
            sizes="(max-width: 258px) 100vw, 258px">
    </section>
    
    

    
    
    

    
    
    

    
    
    
    
</aside>
            </div>
        </div>

        <footer id="site-footer" class="site-footer" role="contentinfo">
    <div class="max-width">
    </div>
    <div class="footer">
        <div id="footercontent">
            © lican.asia All rights reserved<br/>
            Built with Hugo Theme <a href="https://github.com/idoubi/hugo-theme-period" target="_blank">Period</a>
        </div>
    </div>
</footer>

<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e8351b6d4626d5881d439ea1f6184baa";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
</script>
    
    
    </div>
    
</body>

</html>